package com.bootdo.sso;

import com.bootdo.common.config.ApplicationContextRegister;
import com.bootdo.common.utils.HttpUtil;
import com.bootdo.redis.RedisUtil;
import com.bootdo.system.domain.UserDO;
import org.springframework.security.crypto.codec.Base64;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

public abstract class CheckTokenService {

    public void checkToken(String token){
        RedisUtil redisUtil = ApplicationContextRegister.getBean(RedisUtil.class);
        UserDO userDO = (UserDO) redisUtil.get(token);
        if(null != userDO)
            loadLocalUserAuthentication(userDO);
    }

    public abstract  void  loadLocalUserAuthentication(String json);
    public abstract void loadLocalUserAuthentication(UserDO user);

    private   String getAuthorization(String clientId ,String clientSecret){
        String creds = String.format("%s:%s", clientId, clientSecret);
        try {
            return "Basic " + new String(Base64.encode(creds.getBytes("UTF-8")));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return  "";
    }


    /*  Map map = new HashMap<>();
        map.put("token",token);
        String r = "";
        try {
            r =  HttpUtil.checkToken(SsoConfig.check_token_url,map,getAuthorization(SsoConfig.clientId, SsoConfig.clientSecret));
            loadLocalUserAuthentication(r);
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.err.println(r);*/




}
